package application;

import org.apache.wicket.Application;
import org.apache.wicket.protocol.http.BufferedWebResponse;
import org.apache.wicket.protocol.http.WebResponse;

import javax.servlet.http.HttpServletResponse;

// author: victory
//public class CleanWebResponse {
//    public static WebResponse getNew(final Application app, final HttpServletResponse servletResponse) {
//        return app.getRequestCycleSettings().getBufferResponse() ? new Buffered(servletResponse) : new Unbuffered(servletResponse);
//    }
//
//    static class Buffered extends BufferedWebResponse {
//        public Buffered(final HttpServletResponse httpServletResponse) {
//            super(httpServletResponse);
//        }
//
//        @Override
//        public CharSequence encodeURL(final CharSequence url) {
//            return url;

//        }
//    }
//
//    static class Unbuffered extends WebResponse {
//        public Unbuffered(final HttpServletResponse httpServletResponse) {
//            super(httpServletResponse);
//        }
//
//        @Override
//        public CharSequence encodeURL(final CharSequence url) {
//            return url;
//        }
//    }
//}
public class CleanWebResponse {

    public static WebResponse getNew(final Application app, final HttpServletResponse servletResponse) {
        return getNew(app, servletResponse, false);
    }

    public static WebResponse getNew(final Application app, final HttpServletResponse servletResponse, final boolean rewriteResourceRoot) {
        return app.getRequestCycleSettings().getBufferResponse() ? new Buffered(servletResponse, rewriteResourceRoot) : new Unbuffered(
                servletResponse, rewriteResourceRoot);
    }

    static class Buffered extends BufferedWebResponse {

        private boolean rewriteResourceRoot;

        public Buffered(final HttpServletResponse httpServletResponse, final boolean rewriteResourceRoot) {
            super(httpServletResponse);
            this.rewriteResourceRoot = rewriteResourceRoot;
        }

        @Override
        public CharSequence encodeURL(final CharSequence url) {
            String str = String.valueOf(url);
            if (rewriteResourceRoot && str.startsWith("resources/org.apache.wicket")) {
                return "/" + url;
            } else {
                return url;
            }
        }
    }

    static class Unbuffered extends WebResponse {

        private boolean rewriteResourceRoot;

        public Unbuffered(final HttpServletResponse httpServletResponse, final boolean rewriteResourceRoot) {
            super(httpServletResponse);
            this.rewriteResourceRoot = rewriteResourceRoot;
        }

        @Override
        public CharSequence encodeURL(final CharSequence url) {
            String str = String.valueOf(url);
            if (rewriteResourceRoot && str.startsWith("resources/org.apache.wicket")) {
                return "/" + url;
            } else {
                return url;
            }
        }
    }
}
